<%@page import="multiface.web.util.categories.AlberoCategorieRicambi"%>
<%@page import="multiface.web.util.Paginator"%>
<%@page import="multiface.awwadco.jpa.Utente"%>
<%@page import="multiface.web.util.PagerList"%>
<%@page import="javax.persistence.Query"%>
<%@page import="java.util.LinkedList"%>
<%@page import="multiface.awwadco.jpa.PersistenceManager"%>
<%if (session.getAttribute("admin") == null) {
        response.sendRedirect(multiface.web.util.ServletUtils.getBaseUrl(request) + "/admin/index.jsp?err=y");
    }
%>

<%@page import="java.util.Iterator"%>
<%@page import="multiface.awwadco.jpa.Prodotto"%>
<%@page import="java.util.List"%>
<%@page import="javax.persistence.RollbackException"%>
<%@page import="javax.persistence.EntityManager"%>
<%@page import="javax.persistence.Persistence"%>
<%@page import="javax.persistence.EntityManagerFactory"%>
<%@page import="multiface.awwadco.jpa.Categoria"%>
<%@page import="multiface.awwadco.jpa.File"%>
<%@page import="multiface.web.util.Util"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<div class="box">
    <%
        EntityManagerFactory emf = PersistenceManager.getInstance().getEntityManagerFactory();
        EntityManager em = emf.createEntityManager();

        AlberoCategorieRicambi tree = new AlberoCategorieRicambi();
        tree.caricaDaDB();

        //Parametri della jsp
        String order = request.getParameter("order");
        String direction = request.getParameter("direction");
        String text = request.getParameter("text");
        String jsp = "list_part.jsp";
        String functionJS = "open";
        String indexs = request.getParameter("index");
        int index = 0;

        //Controlli sui parametri
        if (indexs != null) {
            index = Integer.parseInt(indexs);
        }
        if (direction == null) {
            direction = "ASC";
        }

        //Paginator
        Paginator paginator = new Paginator();
        paginator.setCSSClass("pager-list");
        paginator.setCSSClassCurrentPage("selected");
        paginator.setCurrentIndex(index);
        paginator.setElementsForPage_fromDB(true);
        paginator.setFunctionJS(functionJS);
        paginator.setPageReturn(jsp);

        //Set Parametri iniziali
        paginator.addParameter("index", index + "");


        List<Prodotto> list = new LinkedList<Prodotto>();
        if (text == null) {
            if (order == null) {
                Query q = em.createNamedQuery("Ricambio.findAll", Prodotto.class);
                paginator.setTotalElements(q.getResultList().size());
                q.setFirstResult(index);
                q.setMaxResults(paginator.getElementsForPage());
                list = q.getResultList();
            } else {
                paginator.addParameter("order", order);
                if (direction.equals("ASC")) {
                    paginator.addParameter("direction", "DESC");
                    Query q = em.createQuery("SELECT p FROM Prodotto p WHERE p.ricambio=true ORDER BY p." + order + " ASC", Prodotto.class);
                    paginator.setTotalElements(q.getResultList().size());
                    q.setFirstResult(index);
                    q.setMaxResults(paginator.getElementsForPage());
                    list = q.getResultList();
                } else {
                    paginator.addParameter("direction", "ASC");
                    Query q = em.createQuery("SELECT p FROM Prodotto p WHERE p.ricambio=true ORDER BY p." + order + " DESC", Prodotto.class);
                    paginator.setTotalElements(q.getResultList().size());
                    q.setFirstResult(index);
                    q.setMaxResults(paginator.getElementsForPage());
                    list = q.getResultList();
                }
            }
        } else {
            paginator.addParameter("text", text);
            if (order == null) {
                Query q = em.createQuery("SELECT p FROM Prodotto p WHERE p.ricambio=true AND (p.nome LIKE :like1 OR p.codice LIKE :like2) ORDER BY p.dataora DESC", Prodotto.class);
                q.setParameter("like1", "%" + text + "%");
                q.setParameter("like2", "%" + text + "%");
                paginator.setTotalElements(q.getResultList().size());
                q.setFirstResult(index);
                q.setMaxResults(paginator.getElementsForPage());
                list = q.getResultList();
            } else {
                paginator.addParameter("order", order);
                if (direction.equals("ASC")) {
                    paginator.addParameter("direction", "DESC");
                    Query q = em.createNativeQuery("SELECT p FROM Prodotto p WHERE p.ricambio=true AND (p.nome LIKE :like1 OR p.codice LIKE :like2) ORDER BY p." + order + " ASC", Prodotto.class);
                    q.setParameter("like1", "%" + text + "%");
                    q.setParameter("like2", "%" + text + "%");
                    paginator.setTotalElements(q.getResultList().size());
                    q.setFirstResult(index);
                    q.setMaxResults(paginator.getElementsForPage());
                    list = q.getResultList();
                } else {
                    paginator.addParameter("direction", "ASC");
                    Query q = em.createNativeQuery("SELECT p.* FROM Prodotto p WHERE p.ricambio=true AND (p.nome LIKE :like1 OR p.codice LIKE :like2) ORDER BY p." + order + " DESC", Prodotto.class);
                    q.setParameter("like1", "%" + text + "%");
                    q.setParameter("like2", "%" + text + "%");
                    paginator.setTotalElements(q.getResultList().size());
                    q.setFirstResult(index);
                    q.setMaxResults(paginator.getElementsForPage());
                    list = q.getResultList();
                }
            }
        }

        Iterator i = list.iterator();
        out.print("<table class=\"listing\" border=\"0\">");
        out.print("<tr>");
        out.print("<td>" + paginator.getFunction("order", "codice", "Code") + "</td>");
        out.print("<td>" + paginator.getFunction("order", "nome", "Name") + "</td>");
        out.print("<td>" + paginator.getFunction("order", "categoriaIdcategoria.idcategoria", "Category") + "</td>");
        out.print("<td>" + paginator.getFunction("order", "anno", "Year") + "</td>");
        out.print("<td>" + paginator.getFunction("order", "pezziMagazzino", "Stock") + "</td>");
        out.print("<td>" + paginator.getFunction("order", "visualizzazioni", "Visits") + "</td>");
        out.print("<td>" + paginator.getFunction("order", "dataora", "Date") + "</td>");
        out.print("<td colspan=\"5\" align=center>Fast Setting</td>");
        out.print("</tr>");
        while (i.hasNext()) {
            Prodotto p = (Prodotto) i.next();

            em.refresh(p);

            String img_promo = "notpromo.png";
            if (p.getOfferta()) {
                img_promo = "promo.png";
            }
            String img_home = "nothome.png";
            if (p.getHomepage()) {
                img_home = "home.png";
            }
            String img_vis = "unvisibile.png";
            if (p.getStato().equals("VISIBILE")) {
                img_vis = "visibile.png";
            }
            String img_sold = "sold_icon_no.png";
            if (p.getVenduto()) {
                img_sold = "sold_icon_yes.png";
            }


            out.print("<tr id=\"prod_" + p.getIdprodotto() + "\"  >");
            out.print("<td class=\"td-code\">" + p.getCodice() + "</td>");
            out.print("<td class=\"td-name\"><a href=\"javascript:open('mod_part.jsp',{pid:'" + p.getIdprodotto() + "'});\">" + p.getNome() + "</a></td>");
            out.print("<td class=\"td-cat\">" + tree.PercorsoName(p.getCategoriaIdcategoria()) + "</td>");
            out.print("<td class=\"td-anno\">" + p.getAnno() + "</td>");
            out.print("<td class=\"td-stock\">" + p.getPezziMagazzino() + "</td>");
            out.print("<td class=\"td-views\">" + p.getVisualizzazioni() + "</td>");
            out.print("<td class=\"td-dataora\">" + Util.FormatData(p.getDataora()) + "</td>");
            //out.print("<td class=\"td-moneta\">" + p.getMoneta() + "</td>");
            out.print("<td class=\"td-fast-setting\"><a title=\"Set Un/Visible\" href=\"javascript:visibile(" + p.getIdprodotto() + ");\"><img id=\"img_visibile_" + p.getIdprodotto() + "\" src=\"../engine/img/" + img_vis + "\"></img></a></td>");
            out.print("<td class=\"td-fast-setting\"><a title=\"Set to HomePage\" href=\"javascript:homepage(" + p.getIdprodotto() + ");\"><img id=\"img_homepage_" + p.getIdprodotto() + "\" src=\"../engine/img/" + img_home + "\"></img></a></td>");
            out.print("<td class=\"td-fast-setting\"><a title=\"Set Promo\" href=\"javascript:promo(" + p.getIdprodotto() + ");\"><img id=\"img_promo_" + p.getIdprodotto() + "\" src=\"../engine/img/" + img_promo + "\"></img></a></td>");
            out.print("<td class=\"td-fast-setting\"><a title=\"Set Un/Sold\" href=\"javascript:sold(" + p.getIdprodotto() + ")\"><img id=\"img_venduto_" + p.getIdprodotto() + "\" src=\"../engine/img/" + img_sold + "\"></img></a></td>");
            out.print("<td class=\"td-fast-setting\"><a title=\"Delete Part\" href=\"javascript:del('part'," + p.getIdprodotto() + ");\"><img id=\"delete_part_" + p.getIdprodotto() + "\" src=\"../engine/img/delete.png\"></img></a></td>");
            out.print("</tr>");
        }
        out.print("</table>");

        //!!!IMPORTANTE!!!! rittere la direzione in quella della pagina attuale
        paginator.addParameter("direction", direction);
        out.print(paginator);

        em.close();
        //emf.close();
    %>

</div>
<script>
    $("tr").hover(
    function(){
        $(this).find(".td-name").append($("<div class=\"tr-edit\">EDIT<div>"));
    },
    function () {
        $(this).find(".td-name .tr-edit").remove();
    }
);
</script>

